#ifndef PLOTTERSETTINGS_H
#define PLOTTERSETTINGS_H

#include<QDebug>

/**
 * @brief 记录每一个放大级别的坐标
 */
class PlotterSettings{
public:
    PlotterSettings(){
        this->minX = 0;
        this->maxX = 10;
        this->minY = 0;
        this->maxY = 10;
        this->numXTicks = 5;
        this->numYTicks = 5;
    }

public:
    double spanX(){
        return maxX - minX;
    }

    double spanY(){
        return maxY - minY;
    }

    void adjust(){
        adjustAxis(minX, maxX, numXTicks);
        adjustAxis(minY, maxY, numYTicks);
    }

    void toString(){
        QString s("%1, %2, %3, %4");
        s = s.arg(minX).arg(maxX).arg(minY).arg(maxY);
        qDebug() << s;
    }

private:
    void adjustAxis(double &min, double &max, int &numTicks){
        const int MinTicks = 4;
        double grossStep = (max - min) / MinTicks;
        double step = std::pow(10.0, std::floor(std::log10(grossStep)));

        if (5 * step < grossStep) {
            step *= 5;
        } else if (2 * step < grossStep) {
            step *= 2;
        }

        numTicks = int(std::ceil(max / step) - std::floor(min / step));
        if (numTicks < MinTicks)
            numTicks = MinTicks;
        min = std::floor(min / step) * step;
        max = std::ceil(max / step) * step;
    }

public:
    double minX;
    double maxX;
    double minY;
    double maxY;
    int numXTicks;
    int numYTicks;
};

#endif // PLOTTERSETTINGS_H
